package com.andy.buyu.server.socket.codec;

import java.io.Serializable;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

public class DataWrap implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private ByteBuf buf;
	
	public DataWrap(ByteBuf buf) {
		this.buf = buf;
	}
	
	public byte getByte(){
		return buf.readByte();
	}
	
	public int getInt(){
		return buf.readInt();
	}
	
	public long getLong(){
		return buf.readLong();
	}
	
	/**
	 * 获取UTF8的字符串
	 * @return
	 */
	public String getString(){
		int length = buf.readShort();
		return (String) buf.readCharSequence(length, CharsetUtil.UTF_8);
	}

	/**
	 * 释放buf
	 */
	public void release() {
		if(buf.refCnt() > 0) {
			ReferenceCountUtil.safeRelease(buf);
		}
	}
	
	
}

